package study.算法Algorithm.排序算法.选择排序;

import java.util.Arrays;

public class SelectSort {

    public static void main(String[] args) {
//        m01();
//        m02();
        m03();
    }

    private static void m03() {
        int arr[] = {101,34,110,1};
        for (int i = 0; i < arr.length - 1; i++) {
            int minIndex = i;
            int min = arr[minIndex];
            for (int j = i+1; j < arr.length; j++) {
                if (min>arr[j]){
                    min = arr[j];
                    minIndex = j;
                }
            }
            if (minIndex!=i){
                arr[minIndex] = arr[i];
                arr[i] = min;
                System.out.println("第" + (i + 1) + "趟排序");
                System.out.println(Arrays.toString(arr));
            }
        }
    }

    private static void m02() {
        int arr[] = {101,34,110,1};
        for (int i = 0; i < arr.length - 1; i++) {
            int minIndex = i;
            int min = arr[minIndex];
            for (int j = i+1; j < arr.length; j++) {
                if (min>arr[j]){
                    min = arr[j];
                    minIndex = j;
                }
            }
            if (minIndex!=i){
                arr[minIndex] = arr[i];
                arr[i] = min;
                System.out.println("第" + (i + 1) + "趟排序");
                System.out.println(Arrays.toString(arr));
            }
        }
    }

    private static void m01() {
        int arr[] = {101, 34, 110, 1};
        for (int i = 0; i < arr.length-1; i++) {
            int minIndex = i;
            int min = arr[minIndex];   //假定当前数是最小值
            for (int j = i+1; j < arr.length ; j++) {
                if (min > arr[j]) {  //说明假定的最小值，并不是最小
                    min = arr[j];  //重置min
                    minIndex = j;  //重置minIndex
                }
            }
            if (minIndex!=i){
                //将最小值与当前值交换位置
                arr[minIndex] = arr[i];
                arr[i] = min;
                System.out.println("第" + (i + 1) + "趟排序");
                System.out.println(Arrays.toString(arr));
            }
        }
    }
}
